// 菜单相关的API接口
// 这里可以替换为实际的API调用

// 模拟数据
let mockMenuData = [
  {
    id: "1",
    icon: "fa fa-file",
    path: "hello-world",
    label: "Hello world==",
    schema: {
      type: "page",
      title: "Hello world",
      body: "初始页面"
    },
    children: []
  }
];

// 获取菜单列表
export const getMenuList = async () => {
  // 模拟网络延迟
  await new Promise(resolve => setTimeout(resolve, 500));
  
  return {
    success: true,
    data: mockMenuData
  };
};

// 新增菜单
export const addMenu = async (menuData: any) => {
  await new Promise(resolve => setTimeout(resolve, 300));
  
  const newMenu = {
    id: Date.now().toString(),
    ...menuData,
    children: []
  };
  
  mockMenuData.push(newMenu);
  
  return {
    success: true,
    data: newMenu
  };
};

// 更新菜单
export const updateMenu = async (menuId: string, updateData: any) => {
  await new Promise(resolve => setTimeout(resolve, 300));
  
  const updateMenuInArray = (menus: any[]): boolean => {
    for (let i = 0; i < menus.length; i++) {
      if (menus[i].id === menuId) {
        menus[i] = { ...menus[i], ...updateData };
        return true;
      }
      if (menus[i].children && menus[i].children.length > 0) {
        if (updateMenuInArray(menus[i].children)) {
          return true;
        }
      }
    }
    return false;
  };
  
  if (updateMenuInArray(mockMenuData)) {
    return {
      success: true,
      data: mockMenuData
    };
  } else {
    return {
      success: false,
      message: '菜单不存在'
    };
  }
};

// 删除菜单
export const deleteMenu = async (menuId: string) => {
  console.log('删除菜单',menuId);
  
  await new Promise(resolve => setTimeout(resolve, 300));
  
  const deleteMenuFromArray = (menus: any[]): boolean => {
    for (let i = 0; i < menus.length; i++) {
      if (menus[i].id === menuId) {
        menus.splice(i, 1);
        return true;
      }
      if (menus[i].children && menus[i].children.length > 0) {
        if (deleteMenuFromArray(menus[i].children)) {
          return true;
        }
      }
    }
    return false;
  };
  
  if (deleteMenuFromArray(mockMenuData)) {
    return {
      success: true
    };
  } else {
    return {
      success: false,
      message: '菜单不存在'
    };
  }
};

// 移动菜单
export const moveMenu = async (menuId: string, targetParentId: string) => {
  await new Promise(resolve => setTimeout(resolve, 300));
  
  // 查找要移动的菜单
  let menuToMove: any = null;
  let sourceParent: any = null;
  
  const findAndRemoveMenu = (menus: any[]): boolean => {
    for (let i = 0; i < menus.length; i++) {
      if (menus[i].id === menuId) {
        menuToMove = { ...menus[i] };
        menus.splice(i, 1);
        return true;
      }
      if (menus[i].children && menus[i].children.length > 0) {
        if (findAndRemoveMenu(menus[i].children)) {
          sourceParent = menus[i];
          return true;
        }
      }
    }
    return false;
  };
  
  if (findAndRemoveMenu(mockMenuData)) {
    // 添加到目标父菜单
    const addToTargetParent = (menus: any[]): boolean => {
      for (let i = 0; i < menus.length; i++) {
        if (menus[i].id === targetParentId) {
          if (!menus[i].children) {
            menus[i].children = [];
          }
          menus[i].children.push(menuToMove);
          return true;
        }
        if (menus[i].children && menus[i].children.length > 0) {
          if (addToTargetParent(menus[i].children)) {
            return true;
          }
        }
      }
      return false;
    };
    
    if (addToTargetParent(mockMenuData)) {
      return {
        success: true
      };
    } else {
      // 如果找不到目标父菜单，恢复到原位置
      if (sourceParent) {
        if (!sourceParent.children) {
          sourceParent.children = [];
        }
        sourceParent.children.push(menuToMove);
      } else {
        mockMenuData.push(menuToMove);
      }
      return {
        success: false,
        message: '目标父菜单不存在'
      };
    }
  } else {
    return {
      success: false,
      message: '要移动的菜单不存在'
    };
  }
};
